package com.zhu.salesmanagement.common;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.zhu.location.R;

import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.os.Handler;
import android.os.Message;

public class GetGpsLocation {

    private final static int MSG_GETLOCATION_WAIT = 2;
    private final static int MSG_GETLOCATION_IGNORE = 3;

    private final static int WAIT_TIME_IGNORE = 25 * 1000;
    private final static int UPDATE_TIME = 5 * 1000;
    private final static double OFFSET_POS = 0.0001;

    private boolean isShowProgressbar = false;
    private Context mContext = null;
    private boolean isShowToast = false;
    private ProgressDialog mProgressDialog = null;
    private int requestCount = 0;
    private boolean isLocationing = false;
    private Location mLastlocation = null;

    private LocationClient mLocationClient = null;
    private LocationClientOption option = null;
    private int mWaitTime = 0;
    private boolean isIgnore = true;

    public BDLocationListener myListener = new MyLocationListenner();

    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null){
                doResult(null);
            }else{
            	int type = location.getLocType();
            	if(Actions.DEBUG){
                	Log.e("GetGpsLocation type="+type);
            	}
            	if(type == BDLocation.TypeGpsLocation || type ==  BDLocation.TypeNetWorkLocation){
                    Location teplocation = new Location("A");
                    teplocation.setLatitude(location.getLatitude());
                    teplocation.setLongitude(location.getLongitude());
                    doResult(teplocation);
            	}
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
            if (poiLocation == null){
                return ;
            }
        }
    }

    private Handler mHandler = new Handler(){
        public void handleMessage(Message msg) {
            switch(msg.what){
                case MSG_GETLOCATION_WAIT:
                    getLocation();
                    break;
                case MSG_GETLOCATION_IGNORE:
                    isIgnore = false;
                    break;
            }
        }
    };

    public GetGpsLocation(Context context, int updatetime){
        mContext = context;
        mLocationClient = new LocationClient(mContext);
        mLocationClient.registerLocationListener(myListener);
        option = new LocationClientOption();
        option.setOpenGps(true);
        option.setAddrType("all");
        option.setCoorType("bd09ll");
        option.setScanSpan(UPDATE_TIME);
        option.setProdName("SalesManager");
        option.disableCache(true);
        option.setPoiNumber(5);
        option.setPoiDistance(1000);
        option.setPoiExtraInfo(true);
        mLocationClient.setLocOption(option);

        mWaitTime = updatetime * 1000;

        isShowProgressbar = false;
        if(isShowProgressbar){
            mProgressDialog = new ProgressDialog(context);
            mProgressDialog.setMessage(context.getString(R.string.getlocationing));
            mProgressDialog.setCancelable(false);
            mProgressDialog.setCanceledOnTouchOutside(false);
        }
        isShowToast = true;
        getLocation();
    }

    public boolean getLocation()
    {
        if(isLocationing){
            return true;
        }
        startGetLocation();
        return true;
    }

    private void startGetLocation(){
        if(Actions.DEBUG){
            Log.e("GetGpsLocation startGetLocation");
        }
        if(mProgressDialog != null){
            mProgressDialog.show();
        }
        start();
        mHandler.sendEmptyMessageDelayed(MSG_GETLOCATION_IGNORE, WAIT_TIME_IGNORE);
        requestCount++;
        isIgnore = true;
        isLocationing = true;
        mLastlocation = null;
    }

    private void doResult(Location location){
        if(Actions.DEBUG){
            Log.e("GetGpsLocation doResult="+location);
        }
        if(mLastlocation == null || location == null){
            mLastlocation = location;
            return;
        }

        if(isMyLocation(location) && !isIgnore){
            if(Actions.DEBUG){
                Log.e("GetGpsLocation doResult=true");
            }
            if(mProgressDialog != null){
                mProgressDialog.cancel();
            }
            stop();
            mHandler.sendEmptyMessageDelayed(MSG_GETLOCATION_WAIT, mWaitTime);
            isLocationing = false;
            if(isShowToast){
                Actions.toastShort(mContext, mContext.getString(R.string.getlocationsuccess, String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude())));
            }
        }else{
            if(Actions.DEBUG){
                Log.e("GetGpsLocation doResult=false");
            }
            mLastlocation = location;
        }
    }

    public void stop(){
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.stop();
        }
    }

    public void start(){
        if (mLocationClient != null && !mLocationClient.isStarted()){
        	if(Actions.DEBUG){
        		Log.e("GetGpsLocation start get logcation");
        	}
        	mLocationClient.start();
        	mLocationClient.requestLocation();
        }
    }

    public void destroy(){
        mHandler.removeMessages(MSG_GETLOCATION_WAIT);
        mHandler.removeMessages(MSG_GETLOCATION_IGNORE);
        stop();
        mLocationClient = null;
        if(mProgressDialog != null){
            mProgressDialog.cancel();
            mProgressDialog = null;
        }
    }

    public boolean isGetLocationing(){
        return isLocationing;
    }

    private boolean isMyLocation(Location location){
        double lastLng = mLastlocation.getLongitude();
        double lastLat= mLastlocation.getLatitude();
        double curLng = location.getLongitude();
        double curLat = location.getLatitude();
        if(Actions.DEBUG){
            Log.e("GetGpsLocation="+Math.abs(curLat - lastLat));
        }
        if(Math.abs(curLat - lastLat) < OFFSET_POS && Math.abs(curLng - lastLng) < OFFSET_POS){
            return true;
        }
        return false;
    }
}
